package com.jxwifi.cloud.quickcleanserver.adapter;

import android.support.annotation.LayoutRes;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.bean.OrderFromBean;
import com.jxwifi.cloud.quickcleanserver.utils.LocationGaoDe;
import com.jxwifi.cloud.quickcleanserver.utils.TokenTimeUtils;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2019/5/13.
 */

public class OrderFormListAdapter extends BaseQuickAdapter<OrderFromBean, BaseViewHolder> {


    public OrderFormListAdapter(@LayoutRes int layoutResId){
        super(layoutResId,null);
    }


    @Override
    protected void convert(BaseViewHolder helper, OrderFromBean item) {
        helper.setText(R.id.tv_form_order_service_time_text,item.getServiceTime());
        helper.setText(R.id.tv_form_order_address_text,item.getProvince() + "" + item.getCity() + item.getArea() + "" + "" + item.getAddress());
        float mestimate = Float.valueOf(item.getPrices()) * item.getExpectHours();
        DecimalFormat myformat = new DecimalFormat("0.00");
        SpannableString styledText = new SpannableString("" + myformat.format(mestimate));
        styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style0), 0, myformat.format(mestimate).indexOf(".") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style1), myformat.format(mestimate).indexOf(".") + 1, myformat.format(mestimate).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_list_order_from_item_income,styledText);
        //double disprovn = DistanceOfTwoPoints(LocationGaoDe.getLatitude(), LocationGaoDe.getLongitude(), Double.valueOf(item.getLat()), Double.valueOf(item.getLon())) / 1000;
        if (item.getRideDistance() > 1) {
            DecimalFormat format = new DecimalFormat("#0.00");
            String str = format.format(item.getRideDistance());
            helper.setText(R.id.tv_list_order_from_item_distance,str);
        } else {
            helper.setText(R.id.tv_list_order_from_item_distance,"<1");
        }
        helper.setText(R.id.tv_form_service_type_text,item.getProductName());
        //换算时间  服务的时间为：serviceTime 系统时间：now TokenTimeUtils.newTime(mAskBean.getServiceTime(),mAskBean.getNow())
        String mServiceTime = TokenTimeUtils.newTime(item.getServiceTime(), item.getNow());
        if (mServiceTime.indexOf(":") != -1) {
            helper.setText(R.id.tv_list_order_from_item_hours,"小时");
            helper.setText(R.id.tv_list_order_from_item_time,mServiceTime);
        } else {
            helper.setText(R.id.tv_list_order_from_item_hours,"天");
            helper.setText(R.id.tv_list_order_from_item_time,mServiceTime);
        }
    }

    private static final double EARTH_RADIUS = 6378137;

    public static double DistanceOfTwoPoints(double lat1, double lng1,
                                             double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
}
